/*
 * @(#)XfslStatusPanel.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.view;

import xfuzzy.util.*;
import xfuzzy.xfsl.model.XfslStatus;

import javax.swing.*;

/**
 * Panel de muestra del estado del aprendizaje
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslStatusPanel extends Box {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603071L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Campos para mostrar los valores
	 */
	private XTextForm text[] = new XTextForm[10];

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslStatusPanel() {
		super(BoxLayout.Y_AXIS);

		String label[] = { "Status", "Iteration  ", "Error", "RMSE", "MxAE",
				"Variation     ", "Error", "RMSE", "MxAE", "Variation     " };

		for(int i=0; i<text.length; i++) {
			text[i] = new XTextForm(label[i]);
			text[i].setEditable(false);
			text[i].setFieldWidth(100);
		}
		XTextForm.setWidth(text);

		Box trnbox = new Box(BoxLayout.Y_AXIS);
		trnbox.add(new XLabel("Training"));
		for(int i=2; i<6; i++) {
			trnbox.add(Box.createGlue());
			trnbox.add(text[i]);
		}

		Box tstbox = new Box(BoxLayout.Y_AXIS);
		tstbox.add(new XLabel("Test"));
		for(int i=6; i<10; i++) {
			tstbox.add(Box.createGlue());
			tstbox.add(text[i]);
		}

		Box box = new Box(BoxLayout.X_AXIS);
		box.add(trnbox);
		box.add(tstbox);

		add(new XLabel("Status"));
		add(Box.createGlue());
		add(text[0]);
		add(Box.createGlue());
		add(text[1]);
		add(Box.createGlue());
		add(box);
		text[0].setText("unconfigured");
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Actualiza el estado al cambiar la configuraci�n
	 */
	public void set(int status) {
		if(status == XfslStatus.UNCONFIGURED) text[0].setText("unconfigured");
		if(status == XfslStatus.READY_TO_RUN) text[0].setText("ready to run");
	}

	/**
	 * Limpia el panel 
	 */
	public void reset(boolean ready) {
		if(ready) text[0].setText("ready to run");
		else text[0].setText("unconfigured");
		for(int i=1; i<text.length; i++) text[i].setText("");
	}

	/**
	 * Modifica las etiquetas seg�n el tipo de sistema
	 */
	public void setTitles(boolean classif) {
		if(classif) {
			text[3].setLabel("Missc. rate");
			text[4].setLabel("Missc. num.");
			text[7].setLabel("Missc. rate");
			text[8].setLabel("Missc. num.");
		} else {
			text[3].setLabel("RMSE");
			text[4].setLabel("MxAE");
			text[7].setLabel("RMSE");
			text[8].setLabel("MxAE");
		}
	}

	/**
	 * Actualiza los campos con los valores del estado
	 */
	public void set(XfslStatus stat) {
		String stname="";
		switch(stat.status) {
		case XfslStatus.UNCONFIGURED: stname = "unconfigured"; break;
		case XfslStatus.READY_TO_RUN: stname = "ready to run"; break;
		case XfslStatus.LEARNING: stname = "learning"; break;
		case XfslStatus.STOPPED: stname = "stopped"; break;
		case XfslStatus.FINISHED: stname = "finished"; break;
		}
		text[0].setText(stname);
		text[1].setText(""+ stat.epoch);
		text[2].setText(round(stat.trn.error));
		text[3].setText(round(stat.trn.rmse));
		text[4].setText(round(stat.trn.mxae));
		text[5].setText(round(stat.trn.var));
		if(stat.testing) {
			text[6].setText(round(stat.tst.error));
			text[7].setText(round(stat.tst.rmse));
			text[8].setText(round(stat.tst.mxae));
			text[9].setText(round(stat.tst.var));
		}
		else for(int i=6; i<10; i++) text[i].setText("");
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Funci�n de redondeo para mostrar un valor real
	 */
	private String round(double dd) {
		String data = ""+dd;
		char[] cdat = data.toCharArray();
		int i;
		for(i=0; i<cdat.length; i++) if(cdat[i]>'0' && cdat[i]<='9') break;
		for(int j=0; j<6 && i<cdat.length; j++,i++)
			if(cdat[i]=='e' || cdat[i]=='E') break;
		StringBuffer buf = new StringBuffer(data.substring(0,i));
		int e = data.indexOf("E");
		if(e == -1) e = data.indexOf("e");
		if(e != -1) buf.append(data.substring(e));
		return buf.toString();
	}
}
